<?php
class ModelHkCompanySetupLayout extends Model {
	public function addCompanySetupLayout($data) {
		$this->event->trigger('pre.admin.vendor_details.add', $data);
		// var_dump($data);exit;

		$query = $this->db->query("INSERT INTO " . DB_PREFIX . "company_setup_home SET title_cn = '" . $this->db->escape($data['title_cn']) . "', title_en = '" . $this->db->escape($data['title_en']) . "', details_cn = '" . $this->db->escape($data['details_cn']) . "', details_en = '" . $this->db->escape($data['details_en']) . "'");
		// $id = $this->db->getLastId();
		// foreach ($data['gd'] as $key => $value) {
		// 	if($value){
		// 		$this->db->query("INSERT INTO " . DB_PREFIX . "comp_director SET img_comp_id = '" . (int)$id . "', img_path = '" . $this->db->escape($value['id_card']) . "', img_addr_path = '" . $this->db->escape($value['address_proof']) . "', img_date = NOW(), img_sort = '" . (int)$key . "'");
		// 		$carid = $this->db->getLastId();
		// 		// var_dump($carid,$id);exit;
		// 	}
		// }
		// foreach ($data['ds'] as $key => $value) {
		// 	if($value){
		// 		$this->db->query("INSERT INTO " . DB_PREFIX . "comp_partner SET img_comp_id = '" . (int)$id . "', img_path = '" . $this->db->escape($value['id_card']) . "', img_addr_path = '" . $this->db->escape($value['address_proof']) . "', img_date = NOW(), img_sort = '" . (int)$key . "'");
		// 	}
		// }
		// $this->event->trigger('post.admin.vendor_details.add', $project_progress_id);

		// return $query;
	}

	public function editCompanySetupLayout($id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "company_setup_home SET title_cn = '" . $this->db->escape($data['title_cn']) . "', title_cn = '" . $this->db->escape($data['title_cn']) . "', details_cn = '" . $this->db->escape($data['details_cn']) . "', details_en = '" . $this->db->escape($data['details_en']) . "' WHERE home_id = '".(int)$id."'");
	}

	public function deleteVendorChecklist($id) {
		

		$this->db->query("DELETE FROM `" . DB_PREFIX . "project_file` WHERE project_file_id in (" . $no_1.",".$no_5.",".$no_6.",".$no_7.",".$no_8.",".$no_9.",".$no_10.",".$no_11.",".$no_12.",".$no_13.",".$no_14.",".$no_15.",".$no_16.",".$no_17.",".(int)$query->row['confirm']. ")");
		$this->db->query("DELETE FROM `" . DB_PREFIX . "project_progress` WHERE project_progress_id = '" . (int)$project_progress_id . "'");

		$this->event->trigger('post.admin.vendor.delete', $project_progress_id);
	}

	public function repairCategories($parent_id = 0) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category WHERE parent_id = '" . (int)$parent_id . "'");

		foreach ($query->rows as $category) {
			// Delete the path below the current one
			$this->db->query("DELETE FROM `" . DB_PREFIX . "category_path` WHERE category_id = '" . (int)$category['category_id'] . "'");

			// Fix for records with no paths
			$level = 0;

			$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "category_path` WHERE category_id = '" . (int)$parent_id . "' ORDER BY level ASC");

			foreach ($query->rows as $result) {
				$this->db->query("INSERT INTO `" . DB_PREFIX . "category_path` SET category_id = '" . (int)$category['category_id'] . "', `path_id` = '" . (int)$result['path_id'] . "', level = '" . (int)$level . "'");

				$level++;
			}

			$this->db->query("REPLACE INTO `" . DB_PREFIX . "category_path` SET category_id = '" . (int)$category['category_id'] . "', `path_id` = '" . (int)$category['category_id'] . "', level = '" . (int)$level . "'");

			$this->repairCategories($category['category_id']);
		}
	}

	public function getCompanySetup($id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "company_setup_home WHERE home_id = '" . (int)$id . "'");
			// if($query->num_rows > 0){
			// $queryDS = $this->db->query("SELECT * FROM " . DB_PREFIX . "comp_partner WHERE img_comp_id = '" . (int)$id . "'");
			
			// $queryGD = $this->db->query("SELECT * FROM " . DB_PREFIX . "comp_director WHERE img_comp_id = '" . (int)$id . "'");
			// }
			// $query->row['data'] = array(
			// 	'ds' => $queryDS->rows,
			// 	'gd' => $queryGD->rows
			// );
		// var_dump($query->row);exit;
		return $query->row;
	}

	public function getCompanySetuplist($data = array()) {
		// var_dump($data);exit;
		// getGroupId
		// var_dump($this->user->getId());exit;
		// $sql = "SELECT pp.*,pc.company_eng_name,pc.company_chn_name,pc.contact_person_eng_name,pc.contact_person_chn_name,pc.contact_person_tel,pc.contact_person_email,ps.sort,vd.english_name_of_supplier,vd.vendor_details_id FROM " . DB_PREFIX . "project_progress pp LEFT JOIN " . DB_PREFIX . "project_customer pc ON (pp.project_customer_id = pc.project_customer_id) LEFT JOIN " . DB_PREFIX . "project_status ps ON (pp.status = ps.project_status_id) LEFT JOIN " . DB_PREFIX ."vendor_details vd ON (pp.vendor_details_id_1 = vd.vendor_details_id) WHERE 1=1 ";

		$sql = "SELECT * FROM `". DB_PREFIX ."company_setup_home` WHERE 1=1 ";
		// $sql = "SELECT * FROM " . DB_PREFIX . "project_progress WHERE 1=1 ";

		if($this->user->getGroupId() != 1 ){
			$sql .= " AND pp.user_id = '" . $this->user->getId() . "'";
		}
		if (!empty($data['filter_ckg_number'])) {
			$sql .= " AND pp.ckg_number LIKE '" . $this->db->escape($data['filter_ckg_number']) . "%'";
		}

		if (!empty($data['filter_project_name'])) {
			$sql .= " AND pp.project_name LIKE '" . $this->db->escape($data['filter_project_name']) . "%'";
		}

		if (!empty($data['filter_english_name_of_supplier'])) {
			$sql .= " AND vd.english_name_of_supplier LIKE '" . $this->db->escape($data['filter_english_name_of_supplier']) . "%'";
		}

		if (!empty($data['filter_customer_name']) && !empty($data['filter_customer_value']) ) {
			$sql .= " AND pc." . $this->db->escape($data['filter_customer_name']) . " LIKE '%" . $this->db->escape($data['filter_customer_value']) . "%'";
		}

		if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
			if($data['filter_status'] != 'all'){
				$sql .= " AND pp.status = '" . (int)$data['filter_status'] . "'"; 
			}
		}

		$sql .= " GROUP BY home_id ";

		$sort_data = array(
			'pp.ckg_number',
			'pp.project_name',
			'pp.project_customer', 
			'pc.'.$this->db->escape($data['filter_customer_name']),
			'pp.status',
			'pp.sort_order'
		);

		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY ps.sort," . $data['sort'];
		} else {
			$sql .= " ORDER BY home_id";
		}

		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else if(isset($data['order']) && ($data['order'] == 'ASC')){
			$sql .= " ASC";
		} else {
			$sql .= " DESC";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		// var_dump($sql,$data['order']);exit;
		$query = $this->db->query($sql);

		return $query->rows;
	}
	public function getPojectCustomers($data = array()) {
		$sql = "SELECT * FROM " . DB_PREFIX . "project_customer WHERE 1=1 AND customer_status = 1 ";

		if (!empty($data['filter_name'])) {
			$sql .= " AND company_eng_name LIKE '" . $this->db->escape($data['filter_name']) . "%'";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}

		$query = $this->db->query($sql);

		// var_dump($query->rows);exit;
		return $query->rows;
	}

	public function getPojectVendor($data = array()) {
		$sql = "SELECT * FROM " . DB_PREFIX . "vendor_details WHERE 1=1 AND vendor_status = 1 ";

		if (!empty($data['filter_name'])) {
			$sql .= " AND english_name_of_supplier LIKE '" . $this->db->escape($data['filter_name']) . "%'";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}

		$query = $this->db->query($sql);

		// var_dump($query->rows);exit;
		return $query->rows;
	}

	public function getVendorChecklists($data = array()) {
		$sql = "SELECT * FROM " . DB_PREFIX . "project_progress WHERE 1=1 ";

		if (!empty($data['filter_ckg_number'])) {
			$sql .= " AND ckg_number LIKE '" . $this->db->escape($data['filter_ckg_number']) . "%'";
		}

		if (!empty($data['filter_project_name'])) {
			$sql .= " AND project_name LIKE '" . $this->db->escape($data['filter_project_name']) . "%'";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}

		$query = $this->db->query($sql);

		// var_dump($query->rows);exit;
		return $query->rows;
	}
	public function getCategoryDescriptions($category_id) {
		$category_description_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_description WHERE category_id = '" . (int)$category_id . "'");

		foreach ($query->rows as $result) {
			$category_description_data[$result['language_id']] = array(
				'name'             => $result['name'],
				'meta_title'       => $result['meta_title'],
				'meta_description' => $result['meta_description'],
				'meta_keyword'     => $result['meta_keyword'],
				'description'      => $result['description']
			);
		}

		return $category_description_data;
	}

	public function getCategoryFilters($category_id) {
		$category_filter_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_filter WHERE category_id = '" . (int)$category_id . "'");

		foreach ($query->rows as $result) {
			$category_filter_data[] = $result['filter_id'];
		}

		return $category_filter_data;
	}

	public function getCategoryStores($category_id) {
		$category_store_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_to_store WHERE category_id = '" . (int)$category_id . "'");

		foreach ($query->rows as $result) {
			$category_store_data[] = $result['store_id'];
		}

		return $category_store_data;
	}

	public function getCategoryLayouts($category_id) {
		$category_layout_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_to_layout WHERE category_id = '" . (int)$category_id . "'");

		foreach ($query->rows as $result) {
			$category_layout_data[$result['store_id']] = $result['layout_id'];
		}

		return $category_layout_data;
	}

	public function getTotalCompanySetup($data = array()) {
		// $sql = "SELECT COUNT(DISTINCT pp.project_progress_id) AS total FROM " . DB_PREFIX . "project_progress pp LEFT JOIN " . DB_PREFIX . "project_customer pc ON (pp.project_customer_id = pc.project_customer_id) LEFT JOIN " . DB_PREFIX . "project_status ps ON (pp.status = ps.project_status_id) LEFT JOIN " . DB_PREFIX ."vendor_details vd ON (pp.vendor_details_id_1 = vd.vendor_details_id) WHERE 1=1 ";
		$sql = "SELECT COUNT(DISTINCT id ) AS total FROM `". DB_PREFIX ."company_setup` WHERE 1=1 ";


		if($this->user->getGroupId() != 1 ){
			$sql .= " AND pp.user_id = '" . $this->user->getId() . "'";
		}
		if (!empty($data['filter_ckg_number'])) {
			$sql .= " AND pp.ckg_number LIKE '" . $this->db->escape($data['filter_ckg_number']) . "%'";
		}

		if (!empty($data['filter_project_name'])) {
			$sql .= " AND pp.project_name LIKE '" . $this->db->escape($data['filter_project_name']) . "%'";
		}

		if (!empty($data['filter_english_name_of_supplier'])) {
			$sql .= " AND vd.english_name_of_supplier LIKE '" . $this->db->escape($data['filter_english_name_of_supplier']) . "%'";
		}

		if (!empty($data['filter_customer_name']) && !empty($data['filter_customer_value']) ) {
			$sql .= " AND pc." . $this->db->escape($data['filter_customer_name']) . " LIKE '%" . $this->db->escape($data['filter_customer_value']) . "%'";
		}

		if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
			$sql .= " AND pp.status = '" . (int)$data['filter_status'] . "'";
		}



		$query = $this->db->query($sql);

		return $query->row['total'];
	}
	
	public function getTotalCategoriesByLayoutId($layout_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "category_to_layout WHERE layout_id = '" . (int)$layout_id . "'");

		return $query->row['total'];
	}
	public function checkData($vendor_1,$vendor_2,$vendor_3){
		$query1 = $this->db->query("SELECT director1,director2,director3,director4,director5,shareholder1,shareholder2,shareholder3,shareholder4,shareholder5 FROM " . DB_PREFIX . "vendor_details WHERE vendor_details_id = '" . $vendor_1."'");

		$query2 = $this->db->query("SELECT director1,director2,director3,director4,director5,shareholder1,shareholder2,shareholder3,shareholder4,shareholder5 FROM " . DB_PREFIX . "vendor_details WHERE vendor_details_id = '" . $vendor_2."'");

		$query3 = $this->db->query("SELECT director1,director2,director3,director4,director5,shareholder1,shareholder2,shareholder3,shareholder4,shareholder5 FROM " . DB_PREFIX . "vendor_details WHERE vendor_details_id = '" . $vendor_3."'");

		$q1 = $query1->row;
		$q2 = $query2->row;
		$q3 = $query3->row;
		$result1 = array_filter(array_intersect($q1,$q2));
		$result2 = array_filter(array_intersect($q1,$q3));
		$result3 = array_filter(array_intersect($q2,$q3));
		// var_dump($result1,$result2,$result3);
		if(!empty($result1)){
			return 1;
		}else if(!empty($result2)){
			return 2;
		}else if(!empty($result3)){
			return 3;
		}else{
			return 0;
		}
	}

	public function clearFile($project_progress_id,$no_name,$name){
		// var_dump($project_progress_id,$no_name,$name);exit;
		if(isset($project_progress_id)){
		$this->db->query("UPDATE " . DB_PREFIX . "project_progress SET `".$no_name."` = ''  WHERE project_progress_id = '" . (int)$project_progress_id."'");
		}
		$query = $this->db->query("SELECT file_url FROM " . DB_PREFIX . "project_file WHERE project_file_id = '" . (int)$name."'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "project_file WHERE project_file_id = '" . (int)$name."'");
		// var_dump($query->row['file_url']);exit;
		return $query->row['file_url'];
	}

	public function getFiles($project_file_id){
		if(!empty($project_file_id)){
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "project_file WHERE project_file_id in (".$this->db->escape($project_file_id).")");
			return $query->rows;
		}else{
			return false;
		}
	}
	public function getProjectStatus(){
	   $query =	$this->db->query("SELECT * FROM " . DB_PREFIX . "project_status WHERE status = 1 ORDER by sort ASC");
	   return $query->rows;
	}
	public function getYbzAddress(){
	   $query =	$this->db->query("SELECT * FROM " . DB_PREFIX . "company_address WHERE status = 1 ORDER by sort ASC");
	   return $query->rows;
	}
	public function getYbzAddressName($company_address_id){
	   $query =	$this->db->query("SELECT * FROM " . DB_PREFIX . "company_address WHERE company_address_id = '" . (int)$company_address_id . "'");
	   return (isset($query->row['address_name']))?$query->row['address_name']:'';
	}	
}
